package com.example.demo1.controller.rest;


import com.example.demo1.common.ResponseCode;
import com.example.demo1.common.ServerResponse;
import com.example.demo1.entity.BaMenu;
import com.example.demo1.service.impl.service.MenuService;
import com.example.demo1.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/menu")
public class MenuRestController {

    @Autowired
    private MenuService menuService;

    @PostMapping("/addMenu")
    public ServerResponse addMenu(HttpServletRequest request, BaMenu menu) {
        String userId;
        try {
            userId = JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")+"";
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "token已过期，请重新登录");
        }
        menu.setUpdateUser(userId);
        return menuService.addMenu(menu);
    }

    @PostMapping("/validateMenuName")
    public ServerResponse validateMenuName(@RequestParam("menuName") String menuName) {
        return menuService.validateMenuName(menuName);
    }

    @PostMapping("/deleteMenu")
    public ServerResponse deleteMenu(@RequestParam("menuId") Integer menuId) {
        return menuService.deleteMenu(menuId);
    }

    @PostMapping("/updateMenu")
    public ServerResponse updateMenu(HttpServletRequest request, BaMenu menu) {
        String userId;
        try {
            userId = JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")+"";
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "token已过期，请重新登录");
        }
        menu.setUpdateUser(userId);
        return menuService.updateMenu(menu);
    }

    @PostMapping("/validateMenuNameExSelf")
    public ServerResponse validateMenuNameExSelf(@RequestParam("menuName") String menuName,
                                                 @RequestParam("menuId") Integer menuId) {
        return menuService.validateMenuNameExSelf(menuName, menuId);
    }

    @PostMapping("/getAllMenus")
    public List<BaMenu> getAllMenus() {
        return menuService.getAllMenus();
    }

    @PostMapping(value = "/getUserRightMenus")
    private ServerResponse<List<BaMenu>> getUserRightMenus(HttpServletRequest request){
        Integer userId;
        try {
            userId = Integer.parseInt(JwtUtil.dececodeToken(request.getHeader("Authorization")).get("userId")+"");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "token已过期，请重新登录");
        }
        return menuService.getUserRightMenus(userId);
    }

}
